#include "ui_trainer.h"

#include <QObject>
#include <QString>
#include <QFileDialog>
#include <QVector>
#include <QTextStream>
#include <QStringList>

#include <time.h>
#include <math.h>


#include <string>

typedef struct{
  QString v1;
  QString v2;
  int trials;
}voc;

class slotshop : public QObject{

  Q_OBJECT
  
  public:
  slotshop(Ui_Trainer *in, QVector<voc> in2){
    win = in;
    QObject::connect(win->actionLoad, SIGNAL(triggered()),
                      this, SLOT(showFileDiag()));
    
    QObject::connect(win->goBtn, SIGNAL(clicked()),this, SLOT(startTraining()));                
    
    win->errLabel->setText("");
    win->lineEdit->setText("");
    win->lineEdit_2->setText("");
    
    words = in2;
    index = 0;
    
    srand(time(0));
  };
  
  public slots:
  void showFileDiag(){
    filename = QFileDialog::getOpenFileName(NULL, tr("Open File"),
                                                 ".",
                                                 tr("Text files (*.txt)"));
    //printf("%s\n", filename.toStdString().c_str());
    if(filename.length()>0){

      QFile file(filename);
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
                              
      
      
      words.clear();
      while(!file.atEnd()) {
        QByteArray line = file.readLine();
        QString sline(line);
        sline = sline.trimmed();
        
        if(sline.length()>4){
          sline.remove(QChar('\n'));
          
          QStringList list1 = sline.split(" - ");
          QStringListIterator javaStyleIterator(list1);
          
          voc v;
          while (javaStyleIterator.hasNext()){
            if(javaStyleIterator.hasNext()) {
							if(win->actionSwitch_languages->isChecked()) v.v1 = javaStyleIterator.next(); 
							else v.v2 = javaStyleIterator.next(); 
						}
            if(javaStyleIterator.hasNext()) {
							if(win->actionSwitch_languages->isChecked()) v.v2 = javaStyleIterator.next(); 
							else v.v1 = javaStyleIterator.next();
						}
          }
          v.trials=0;
          words.append(v);
        }
      }
       
      allwords = QVector<voc>(words);
			                         
      win->errLabel->setText("Type in tha lucky number!");
      win->lineEdit->setText("");
      win->lineEdit_2->setText("");
      win->goBtn->setEnabled(true);
      win->goBtn->setText("Start Training");
      start = true;
      trials = 2;
    }
  }
	
	void startTraining(){
  
    /*for(int i=0; i<words.size();i++){
      printf("%s\n",words[i].v1.toStdString().c_str());
    }*/
    
    if(words.size()>0){
    
      /*printf("%s %s %i \n", 
        win->lineEdit_2->text().trimmed().toLower().toStdString().c_str(), 
        words[index].v2.trimmed().toLower().toStdString().c_str(),
        words[index].trials
        );*/
      
      if(!start){
        if(win->lineEdit_2->text().trimmed().toLower()==words[index].v2.trimmed().toLower()){
          printf("good\n");
          win->errLabel->setText(words[index].v2+": fine! next!");
          
          words[index].trials--;
          
          if(words[index].trials<0){
            words.remove(index);
          }
        }else if(win->lineEdit_2->text().trimmed().toLower()!=words[index].v2.trimmed().toLower()){
          printf("bad\n");
          win->errLabel->setText("aaarrrg! seems to be false! it is: "+words[index].v2+". try again!");
        }
      }
      
      if(words.size()>0){
        index = rand()%words.size();
        win->goBtn->setText("Next");
        win->lineEdit->setText(words[index].v1);
        win->lineEdit_2->setText("");
      }
    else if(words.size()<=0 && trials>0){
		
			words = QVector<voc>(allwords);
			index = rand()%words.size();
      win->goBtn->setText("Next");
      win->lineEdit->setText(words[index].v1);
      win->lineEdit_2->setText("");
      trials--;
			
		}
    }else if(words.size()<=0 && trials<=0){
      win->errLabel->setText("Training is over now. :)");
      win->lineEdit->setText("");
      win->lineEdit_2->setText("");
      win->goBtn->setEnabled(false);
    }
    
    start=false;
  };
  
  private:
  Ui_Trainer *win;
  QString filename;
  QVector<voc> words;
  QVector<voc> allwords;
  int index;
  bool start;
  int trials;
  
  
};
